﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DetectiveCluedo
{
    public partial class CardEditorForm : Form
    {
        private ECardType _cardType;

        public CardEditorForm(ECardType cardType)
        {
            InitializeComponent();

            this._cardType = cardType;
            buttonSetColor.Visible = cardType == ECardType.Suspect;
        }

        private void CardEditorForm_Load(object sender, EventArgs e)
        {
            this.Text = this._cardType.ToString();
            col1.HeaderText = this._cardType.ToString();
            load();
        }

        private void CardEditorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //save();
        }

        private void load()
        {
            Card[] cards = null;

            dataGridView1.Rows.Clear();

            switch (this._cardType)
            {
                case ECardType.Suspect: cards = CluedoGame.Cards.Where(c => c.Type == ECardType.Suspect).ToArray(); break;
                case ECardType.Weapon: cards = CluedoGame.Cards.Where(c => c.Type == ECardType.Weapon).ToArray(); break;
                case ECardType.Room: cards = CluedoGame.Cards.Where(c => c.Type == ECardType.Room).ToArray(); break;
            }

            foreach (Card card in cards)
            {
                int ri = dataGridView1.Rows.Add(card.Name);
                dataGridView1.Rows[ri].Cells[0].Style.BackColor = card.Color;
            }
        }

        private void save()
        {
            //List<string> items = new List<string>();
            string csv = "";

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                {
                    if (csv.Length > 0)
                        csv += ",";

                    if(_cardType == ECardType.Suspect)
                        csv += row.Cells[0].Value.ToString() + "[" + row.Cells[0].Style.BackColor.ToArgb() + "]";
                    else
                        csv += row.Cells[0].Value.ToString();
                }
            }

            switch (this._cardType)
            {
                case ECardType.Suspect: Properties.Settings.Default.Suspects = csv; break;
                case ECardType.Weapon: Properties.Settings.Default.Weapons = csv; break;
                case ECardType.Room: Properties.Settings.Default.Rooms = csv; break;
            }

            Properties.Settings.Default.Save();
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            toolStripStatusLabel1.Text = (dataGridView1.Rows.Count - 1).ToString();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            save();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            save();
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonSetColor_Click(object sender, EventArgs e)
        {

            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (dataGridView1.CurrentRow.Index != -1 && !dataGridView1.CurrentRow.IsNewRow)
                    dataGridView1.CurrentRow.Cells[0].Style.BackColor = cd.Color;
            }
        }

        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
        {
            buttonSetColor.Enabled = !dataGridView1.CurrentRow.IsNewRow;
        }
    }
}
